package com.link2room.cms.controller.api;

import com.link2room.cms.dto.param.base.ArticleBaseParamDto;
import com.link2room.cms.dto.result.ArticleBaseDto;
import com.link2room.cms.facade.IArticleFacadeService;
import com.link2room.common.dto.result.base.BaseResultDto;
import com.link2room.common.jpa.result.QueryResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author sqj88
 * @date 2017/12/1 16:14
 */
@Api(tags= "文章")
@RestController
@RequestMapping(value = "/api/Articles")
public class ArticleApiController {
    @Resource
    private IArticleFacadeService articleFacadeService;

    @ApiOperation(value = "获取文章列表", response = ArticleBaseDto.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name="categoryCode",value="栏位代码",dataType="String", paramType = "query",example="news"),
            @ApiImplicitParam(name="isHalt",value="是否停用",dataType="String", paramType = "query")})
    @RequestMapping(method = RequestMethod.GET)
    public BaseResultDto listArticle(
            @RequestParam(value = "categoryCode" ,required = false) String categoryCode,
            @RequestParam(value = "articleType" ,required = false) String articleType,
            @RequestParam(value = "keyword" ,required = false) String keyword,
            @RequestParam(value = "isHalt" ,required = false) String isHalt
    ) throws Exception {
        ArticleBaseParamDto articleBaseParamDto=new ArticleBaseParamDto();
        articleBaseParamDto.setCategoryCode(categoryCode);
        articleBaseParamDto.setIsHalt(isHalt);
        articleBaseParamDto.setArticleType(articleType);
        articleBaseParamDto.setKeyword(keyword);
        QueryResult<ArticleBaseDto> articleBaseDtoQueryResult = articleFacadeService.listArticles(articleBaseParamDto);
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setQueryResult(articleBaseDtoQueryResult);
        return l2rRs;
    }

    @ApiOperation(value = "获取文章详情", response = ArticleBaseDto.class)
    @ApiImplicitParam(name="id",value="文章ID",dataType="Long", paramType = "path",example="12")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public BaseResultDto article(
            @PathVariable("id") Long id
    ) throws Exception {
        ArticleBaseParamDto articleBaseParamDto=new ArticleBaseParamDto();
        articleBaseParamDto.setId(id);
        ArticleBaseDto articleBaseDto = articleFacadeService.getArticle(articleBaseParamDto);
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setResultInfo(articleBaseDto);
        return l2rRs;
    }
}
